<!-- ParentComponent.vue -->
<script setup>
import { EventBusStore } from '@/stores/eventBusStore';
import { onMounted, onUnmounted } from 'vue';
import ChildComponent from './ChildComponent.vue';

const eventBus = EventBusStore();

// 订阅关闭弹窗事件
let unsubscribe;

onMounted(() => {
  
  unsubscribe = eventBus.subscribe('modalClose', (payload) => {
    console.log('收到关闭弹窗请求，原因:', payload);
    eventBus.closeModal();
  });
});

onUnmounted(() => {
  if (unsubscribe) unsubscribe();
});

function openChildModal() {
  eventBus.openModal();
}
</script>

<template>
  <button @click="openChildModal">打开子组件弹窗</button>
  
  <ChildComponent v-if="eventBus.isModalOpen" />
</template>